<template>
  <div class="wrapper">
    <el-card style="margin: 200px auto; background-color: #fff; width: 400px; height: 370px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>登 录</b></div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="username">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input size="medium" style="margin: 10px 0;width: 175px; float: left"  v-model="user.code"></el-input>
          <el-image class="captchaImg" :src="captchaImg" @click="getCaptcha"></el-image>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: right">
          <el-button type="primary" size="small"  autocomplete="off" @click.prevent="btnLogin($event)">登录</el-button>
          <el-button type="warning" size="small"  autocomplete="off" @click="$router.push('/register')">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import qs from 'qs'

export default {
  name: 'Login',
  data(){
    return{
      user: {
        username: 'admin',
        password: '123',
        code: '',
        token: ''
      },
      captchaImg: '',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入校验码', trigger: 'blur' },
          { min: 5, max: 5, message: '5个字符', trigger: 'blur' }
        ]
      },
    }
  },
  created() {
    var that = this;
    document.onkeydown = function(e) {
      var key = window.event.keyCode;
      if (key === 13 ) {
        that.login();
      }
    }
    this.getCaptcha()
  },
  methods: {
    //登录
    btnLogin(evt){
      this.ablur(evt)
      this.login()
    },
    login() {
      this.$refs['userForm'].validate((valid) => {
        if (valid){
          this.request.post('/login?'+qs.stringify(this.user)).then(res => {
            const jwt = res.headers['authorization']
            // 将jwt存储到应用store中
            this.$store.commit("SET_TOKEN", jwt)
            this.$router.push("/manage")
          })
        }else {
          this.getCaptcha();
          return false;
        }
      });
    },
    //强制失去焦点
    ablur(evt){
      const target = evt.target
      if (target.nodeName === 'SPAN') {
        target.parentNode.blur()
      }
      target.blur()
    },
    getCaptcha(){
      this.request.get('/captcha').then(res => {
        this.user.token = res.data.data.token
        this.captchaImg = res.data.data.captchaImg
      })
    }
  }
}
</script>

<style>
.wrapper {
  background-image: url();
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 5%;
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
  width: 100px;
  margin-top: 10px;
}
</style>
